package com.farm.authority.action;

import com.farm.authority.domain.Organization;
import com.farm.authority.domain.Post;
import com.farm.authority.domain.User;
import com.farm.authority.service.UserServiceInter;

import com.farm.web.WebSupport;
import com.farm.web.easyui.EasyUiUtils;

import java.util.List;
import java.util.Map;
import com.farm.core.page.CommitType;
import com.farm.core.page.PageSet;
import com.farm.core.page.PageType;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.web.spring.BeanFactory;

/* *
 *功能：用户控制层
 *详细：
 *
 *版本：v0.1
 *作者：王东
 *日期：20141119144919
 *说明：
 */
public class UserAction extends WebSupport {
	private Map<String, Object> jsonResult;// 结果集合
	private DataQuery query;// 条件查询
	private User entity;// 实体封装
	private PageSet pageset;// 请求状态
	private String ids;// 主键集合
	private String passwordl;// 原始密码
	private String passwordn1;// 新密码
	private List<Post> posts;
	private Organization organization;

	/**
	 * 查询结果集合
	 * 
	 * @return
	 */
	public String queryall() {
		try {
			query = EasyUiUtils.formatGridQuery(getRequest(), query);
			DataResult result = userIMP.createUserSimpleQuery(query).search();
			result.runDictionary("0:禁用,1:可用,2:删除", "STATE");
			result.runDictionary("1:系统用户,2:其他,3:超级用户", "TYPE");
			result.runformatTime("LOGINTIME", "yyyy-MM-dd HH:mm:ss");
			jsonResult = EasyUiUtils.formatGridData(result);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	/**
	 * 修改密码
	 * 
	 * @return
	 */
	public String editPassword() {
		try {
			if (!userIMP.editLoginPassword(getCurrentUser().getLoginname(),
					passwordl, passwordn1)) {
				throw new RuntimeException("密码修改失败！");
			}
			pageset = new PageSet(PageType.OTHER, CommitType.TRUE, "密码修改成功!");
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 查询组织机构用户结果集合
	 * 
	 * @return
	 */
	public String queryOrgUser() {
		try {
			query = EasyUiUtils.formatGridQuery(getRequest(), query);
			query.addRule(new DBRule("a.ORGANIZATIONID", ids, "="));
			DataResult result = userIMP.createUserPostQuery(query).search();
			result.runDictionary("0:禁用,1:可用,2:删除", "USERSTATE");
			result.runDictionary("1:标准岗位,2:临时岗位", "TYPE");
			result.runDictionary("0:禁用,1:可用,2:删除", "a.STATE");
			result.runformatTime("LOGINTIME", "yyyy-MM-dd HH:mm:ss");
			jsonResult = EasyUiUtils.formatGridData(result);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	/**
	 * 提交修改数据
	 * 
	 * @return
	 */
	public String editSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			entity = userIMP.editUserEntity(entity, getCurrentUser());
			pageset = new PageSet(PageType.UPDATE, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.UPDATE,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 提交新增数据
	 * 
	 * @return
	 */
	public String addSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			entity = userIMP.insertUserEntity(entity, getCurrentUser());
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.ADD,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 删除数据
	 * 
	 * @return
	 */
	public String delSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			for (String id : parseIds(ids)) {
				userIMP.deleteUserEntity(id, getCurrentUser());
			}
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.DEL,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 密码初始化
	 * 
	 * @return
	 */
	public String initPassWord() {
		try {
			for (String id : parseIds(ids)) {
				userIMP.initDefaultPassWord(id, getCurrentUser());
			}
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.DEL,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 跳转
	 * 
	 * @return
	 */
	public String forSend() {
		// TODO 自动生成代码,修改后请去除本注释
		return SUCCESS;
	}

	/**
	 * 显示详细信息（修改或浏览时）
	 * 
	 * @return
	 */
	public String view() {
		try {
			switch (pageset.getPageType()) {
			case (1): {// 新增
				return SUCCESS;
			}
			case (0): {// 展示
				entity = userIMP.getUserEntity(ids);
				posts = userIMP.getUserPosts(ids);
				organization=userIMP.getUserOrganization(ids);
				return SUCCESS;
			}
			case (2): {// 修改
				entity = userIMP.getUserEntity(ids);
				return SUCCESS;
			}
			default:
				break;
			}
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.OTHER,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	private final static UserServiceInter userIMP = (UserServiceInter) BeanFactory
			.getBean("farm_authority_user_ProxyId");

	// ----------------------------------------------------------------------------------
	public DataQuery getQuery() {
		return query;
	}

	public void setQuery(DataQuery query) {
		this.query = query;
	}

	public User getEntity() {
		return entity;
	}

	public void setEntity(User entity) {
		this.entity = entity;
	}

	public PageSet getPageset() {
		return pageset;
	}

	public void setPageset(PageSet pageset) {
		this.pageset = pageset;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Map<String, Object> getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(Map<String, Object> jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String getPasswordl() {
		return passwordl;
	}

	public void setPasswordl(String passwordl) {
		this.passwordl = passwordl;
	}

	public String getPasswordn1() {
		return passwordn1;
	}

	public void setPasswordn1(String passwordn1) {
		this.passwordn1 = passwordn1;
	}

	public List<Post> getPosts() {
		return posts;
	}

	public void setPosts(List<Post> posts) {
		this.posts = posts;
	}
	
	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	// private static final Logger log = Logger.getLogger(UserAction.class);
	private static final long serialVersionUID = 1L;
}
